package com.xm2.demo.login.user.controller;

import com.sun.org.apache.xpath.internal.operations.Mod;
import com.xm2.demo.login.user.service.UserService;
import com.xm2.demo.login.user.vo.*;
import com.xm2.demo.model.Form;
import com.xm2.demo.model.User;
import com.xm2.demo.system.order.ov.OrderPage;
import com.xm2.demo.system.order.service.OrderService;
import com.xm2.demo.utils.JsonResult;
import com.xm2.demo.utils.Md5;
import com.xm2.demo.utils.Pagination;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.ParseException;
import java.util.List;

/**
 * @Create
 * @create 2018-12-20 14:59
 * @Description：
 */
@Controller
@RequestMapping(value = "/user/")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    @Qualifier("orderService")
    private OrderService orderService;

    @GetMapping(value = "login")
    public String login() {
        return "login";
    }

    @PostMapping(value = "login")
    @ResponseBody
    public JsonResult doLogin(@RequestBody LoginVo loginVo, HttpServletRequest request) {
        if (loginVo.getUsername().equals("")) {
            return new JsonResult("101", "用户名不能为空!");
        } else if (loginVo.getPassword().equals("")) {
            return new JsonResult("102", "密码不能为空!");
        } else {
            //根据用户名查询
            User user = userService.findUserByUsername(loginVo.getUsername());
            if (user != null) {
                if (user.getPassword().equals(Md5.getMD5(loginVo.getPassword()))) {
                    request.getSession().setAttribute("user", user);
                    return new JsonResult();
                } else {
                    return new JsonResult("103", "用户名或密码错误!");
                }
            }
            return new JsonResult("103", "用户名或密码错误!");
        }
    }

    /*
     * description:退出
     * @params [request]
     * @return java.lang.String
     */
    @GetMapping(value = "logout")
    public String logout(HttpServletRequest request) {
        User user = (User) request.getSession().getAttribute("user");
        if (user != null) {
            request.getSession().invalidate();
        }
        return "redirect:/user/login";
    }

    /*
     * description:用户注册
     * @params [registerVo]
     * @return com.xm2.demo.utils.JsonResult
     */
    @PostMapping(value = "register")
    @ResponseBody
    public JsonResult register(@RequestBody RegisterVo registerVo) {
        if (registerVo.getUsername().equals("")) {
            return new JsonResult("101", "用户名不能为空!");
        } else if (registerVo.getPassword().equals("")) {
            return new JsonResult("102", "密码不能为空!");
        } else if (registerVo.getName().equals("")) {
            return new JsonResult("104", "姓名不能为空!");
        } else if (registerVo.getPhone().equals("")) {
            return new JsonResult("105", "手机号不能为空!");
        } else {
            User user = userService.findUserByUsername(registerVo.getUsername());
            if (user != null) {
                return new JsonResult("106", "用户名已存在");
            } else {
                userService.save(registerVo);
            }
        }
        return new JsonResult();
    }

    /*
     * description:用户修改密码
     * @params [updateVo]
     * @return com.xm2.demo.utils.JsonResult
     */
    @PostMapping(value = "update")
    @ResponseBody
    public JsonResult updatePassword(@RequestBody UpdateVo updateVo) {
        if (updateVo.getUserNew().equals("")) {
            return new JsonResult("101", "用户名不能为空!");
        } else if (updateVo.getPassOld().equals("")) {
            return new JsonResult("102", "原密码不能为空!");
        } else if (updateVo.getPassNew().equals("")) {
            return new JsonResult("102", "新密码不能为空!");
        } else {
            User user = userService.findUserByUsername(updateVo.getUserNew());
            if (user == null) {
                return new JsonResult("104", "用户名不存在!");
            } else {
                if (!user.getPassword().equals(Md5.getMD5(updateVo.getPassOld()))) {
                    return new JsonResult("103", "用户名或密码错误!");
                } else {
                    //修改密码
                    user.setPassword(Md5.getMD5(updateVo.getPassNew()));
                    userService.updatePassword(user);
                    return new JsonResult();
                }
            }
        }
    }

    /*
     * description:进入客户界面
     * @params [request]
     * @return java.lang.String
     */
    @GetMapping(value = "order")
    public String userOrder(HttpServletRequest request, Model model) {
        User user = (User) request.getSession().getAttribute("user");
        if (user == null) {
            return "redirect:/user/login";
        }
        User userInfo = userService.findUserByUsername(user.getUsername());
        model.addAttribute("userInfo", userInfo);
        Pagination pagination = new Pagination();
        pagination = orderService.findByUsername(user.getUsername(),pagination);
        model.addAttribute("pagination", pagination);
        return "customer";
    }
    /**
     * 条件分页查询
     * @return java.lang.String
     *
     */
    @PostMapping(value = "order")
    @ResponseBody
    public Pagination doSelectOrder(@RequestBody OrderPage orderPage) throws ParseException {
        if(orderPage.getOrderId()!= ""){
            return orderService.findByOrderId(orderPage);
        }else{
            return orderService.findByOrderPagination(orderPage);
        }
    }
    @PostMapping(value = "recharge")
    @ResponseBody
    public JsonResult recharge(@RequestBody RechargeDto rechargeDto) {
        if (rechargeDto.getMoney().equals("")) {
            return new JsonResult("107", "充值金额不能为空!");
        } else {
            String money = rechargeDto.getMoney();
            Double balance;
            try {
                balance = Double.parseDouble(money);
            } catch (NumberFormatException e) {
                return new JsonResult("108", "请填写正确的金额!");
            }
            userService.rechargeByUserId(rechargeDto.getUserId(), balance);
            return new JsonResult("0", "充值成功");
        }
    }

    @PostMapping(value = "findCenter")
    @ResponseBody
    public JsonResult findCenter(@RequestBody String address) {
        if (address.equals("")) {
            return new JsonResult("109", "地址不能为空!");
        } else {
            if (!address.equals("------")) {
                // 根据地址查询洗衣中心
                List<String> lists = userService.findCenter(address);
                if (lists.size() == 0) {
                    return new JsonResult("110", "该地区还没有洗衣中心!");
                } else {
                    return new JsonResult(lists);
                }
            } else {
                return new JsonResult("110", "该地区还没有洗衣中心!");
            }
        }
    }

    /*
     * description:添加预约订单
     * @params [orderDto]
     * @return com.xm2.demo.utils.JsonResult
     */
    @PostMapping(value = "orderPay")
    @ResponseBody
    public JsonResult orderPay(@RequestBody OrderDto orderDto,HttpServletRequest request){
        if (orderDto.getOrderAddress().equals("")){
            return new JsonResult("111","请填写地址!");
        } else if (orderDto.getMoney().equals("")){
            return new JsonResult("112","支付金额不能为空!");
        } else if (orderDto.getCenter().equals("")){
            return new JsonResult("113","洗衣中心不能为空!");
        } else {
            String money = orderDto.getMoney();
            Double payment;
            try {
                payment = Double.parseDouble(money);
            } catch (NumberFormatException e) {
                return new JsonResult("108", "请填写正确的金额!");
            }
            User user = (User) request.getSession().getAttribute("user");
            int count = userService.payOrder(orderDto,payment,user);
            if (count == 1){
                return new JsonResult();
            } else {
                return new JsonResult("114","余额不足!");
            }
        }

    }

    @PostMapping(value = "getState")
    @ResponseBody
    public List<StateDto> getState(@RequestBody String orderId){
        List<StateDto> stateDtoList = userService.getStateRecord(orderId);
        return stateDtoList;
    }
}
